
//选项开关
//点击select
$(document).on("click",".select_box",function(){
	$(this).focus();
	var $thisSelect = $(this).parents(".form_select");
	if($thisSelect.find("ul").is(":hidden")){
		showOptions($thisSelect);
	}else{
		hideOptions($thisSelect);
	}
});
/*//失去焦点时隐藏选项
$(document).on("blur",".select_box",function(){
	var $thisSelect = $(this).parents(".form_select");
	hideOptions($thisSelect);
});*/

$(document).on("mouseleave",".form_select",function(){
	$(this).find(".select_box").on("blur.hideOption",function(){
		var $thisSelect = $(this).parents(".form_select");
		hideOptions($thisSelect);
	});
});
// 
$(document).on("mouseenter",".form_select",function(){
	$(this).find(".select_box").off("blur.hideOption");
});


//鼠标hover
$(document).on("mousemove",".form_select li",function(){
	$(this).parents(".form_select").find("li").removeClass("currentOption");
	$(this).addClass("currentOption");
});


//打开选项
function showOptions(obj){
	//显示选项
	obj.find("ul").show();
	//当前值高亮
	var currentValue = obj.find(".select_box").text()
	obj.find("li").each(function(){
		$(this).removeClass("currentOption");
		if($(this).text() == currentValue){
			$(this).addClass("currentOption");
		}
	});
}
//关闭选项
function hideOptions(obj){
	//隐藏选项
	obj.find("ul").hide();
	//obj.find(".select_box").focus();
}


//选值
//键盘操作
$(document).on("keyup",".select_box",function(e){
	if(e.keyCode == 38 || e.keyCode == 40 ){
		var $thisSelect = $(this).parents(".form_select");
		var currentIndex = $thisSelect.find(".currentOption").index();
		var $options = $thisSelect.find("li");
		var optionSize = $options.size();
		var $newCurrentOption;
		if(e.keyCode == 38){
			if(currentIndex != 0){
				$newCurrentOption = $options.eq(currentIndex - 1);
			}else{
				return;
			}		
		};
		if(e.keyCode == 40){
			if(currentIndex != optionSize - 1){
				$newCurrentOption = $options.eq(currentIndex + 1);
			}else{
				return;
			}
		};
		var newValue = $newCurrentOption.text();
		$options.removeClass("currentOption");
		$newCurrentOption.addClass("currentOption");
		$thisSelect.find(".select_box").html(newValue);
		$thisSelect.find("input").val(newValue);
	}else if(e.keyCode == 13){
		var $thisSelect = $(this).parents(".form_select");
		if($thisSelect.find("ul").is(":hidden")){
			return;
		}
		var newValue = $thisSelect.find(".currentOption").text();
		$thisSelect.find(".select_box").html(newValue);
		$thisSelect.find("input").val(newValue);
		hideOptions($thisSelect); 
	}
});
//鼠标点击
$(document).on("click",".form_select li",function(){
	var $this = $(this);
	var $thisSelect = $this.parents(".form_select");
	var newValue = $this.text();
	$thisSelect.find(".select_box").html(newValue);
	$thisSelect.find("input").val(newValue);
	hideOptions($thisSelect);
});



//元素外单击隐藏
/*$(document).mouseup(function(event){
    if($(event.target).parents("A").length==0){
        $("A").hide();
    }
})
*/

